package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.Student;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController                                                                         //=@Controller+@ResponseBody
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Log
    @GetMapping("/students")
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize,
                       String name, Integer gender) {
        log.info("查询所有学生   --->>>   查询信息：page:{},pageSize:{},name:{},gender:{}", page, pageSize, name, gender);
        PageBean list = studentService.list(page, pageSize, name, gender);
        log.info("查询所有学生成功！");
        return Result.success(list);
    }
    @Log
    @DeleteMapping("/students/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除学生   --->>>   被删除学生的主键：{}",id);
        studentService.deleteById(id);
        log.info("删除学生成功！");
        return Result.success();
    }
    @Log
    @PostMapping("/students")
    public Result add(@RequestBody Student student){
        log.info("添加学生   --->>>   添加的学生信息：{}",student);
        studentService.add(student);
        log.info("添加学生成功！");
        return Result.success(student.getId());                                         //插入数据返回主键
    }
    @Log
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        log.info("修改学生   --->>>   修改后的信息：{}",student);
        studentService.update(student);
        log.info("修改学生成功！");
        return Result.success();
    }

}
